/**
 * Created by hcadmin on 16/4/18.
 */
define([
    "jquery",
    "IO",
    "UPLOADUI",
    "UPLOAD",
    "jqueryUI",
    "bootbox"
], function ($, IO, upui, up, jui, bootbox, definebt) {
    $(function () {
        var realname= $("#realname").val();
        var status=$("#status").val();
        if(status=="-1"){
            status="";
        }
        gettable(realname,status);
        $("#select-btn").bind("click", function () {
            var realname= $("#realname").val();
            var status=$("#status").val();
            if(status=="-1"){
                status="";
            }
            gettable(realname,status);
        });
    });

    function gettable(realname,status){

        var grid;
        //每列数据列性
        var columns = [
            {
                title: '申请人姓名',
                dataIndex: 'realname',
                dataSource: function (data, datarow, gridobj, current_column) {
                    return "<span>" + data[datarow].realname + "</span>";
                    //return "<span><img src='" + data[datarow].user_name + "' width='80px;' height='80px;'>&nbsp;&nbsp;</span>" + data[datarow].title + "</span>";
                }
            },
            {
                title: ' 注册电话',
                dataIndex: 'phone',
                dataSource: function (data, datarow, gridobj, current_column) {
                    return "<span>" + data[datarow].phone + "</span>";
                    //return "<span><img src='" + data[datarow].user_name + "' width='80px;' height='80px;'>&nbsp;&nbsp;</span>" + data[datarow].title + "</span>";
                }
            },
            {
                title: '医院所在省',
                dataIndex: 'province_name',
                dataSource: function (data, datarow, gridobj, current_column) {

                    return "<span>" +province_nameShow(data[datarow].province_id) + "</span>";
                    //return "<span><img src='" + data[datarow].user_name + "' width='80px;' height='80px;'>&nbsp;&nbsp;</span>" + data[datarow].title + "</span>";
                }
            },
            {
                title: '所在市',
                dataIndex: 'city_name',
                dataSource: function (data, datarow, gridobj, current_column) {

                    return "<span>" + city_nameShow(data[datarow].province_id,data[datarow].city_id )+ "</span>";
                    //return "<span><img src='" + data[datarow].user_name + "' width='80px;' height='80px;'>&nbsp;&nbsp;</span>" + data[datarow].title + "</span>";
                }
            },
            {
                title: '所在区',
                dataIndex: 'area_name',
                dataSource: function (data, datarow, gridobj, current_column) {

                    return "<span>" +area_nameShow(data[datarow].province_id,data[datarow].city_id,data[datarow].area_id) + "</span>";
                    //return "<span><img src='" + data[datarow].user_name + "' width='80px;' height='80px;'>&nbsp;&nbsp;</span>" + data[datarow].title + "</span>";
                }
            },
            {
                title: '医院名称',
                dataIndex: 'hospital_name',
                dataSource: function (data, datarow, gridobj, current_column) {

                    if(data[datarow]['hospital']==null){
                        return "<span>undefined</span>";

                    }else{
                        return "<span>" + data[datarow].hospital.hospital_name+ "</span>";
                    }
                }
            },
            {
                title: '所在科室',
                dataIndex: 'department_name',
                dataSource: function (data, datarow, gridobj, current_column) {
                    if(data[datarow]['department']==null){
                        return "<span>undefined</span>";
                    }else{
                        return "<span>" + data[datarow]['department'].department_name + "</span>";
                    }

                    //return "<span><img src='" + data[datarow].user_name + "' width='80px;' height='80px;'>&nbsp;&nbsp;</span>" + data[datarow].title + "</span>";
                }
            },
            {
                title: ' 审核状态',
                dataIndex: 'status',
                dataSource: function (data, datarow, gridobj, current_column) {
                    if(data[datarow].status==0){//状态 0=待审核 1=通过 2=拒绝
                        return "<span>待审核</span>";
                    }
                    else if (data[datarow].status==1){
                        return "<span>通过</span>";
                    }
                    else if(data[datarow].status==2){
                        return "<span>拒绝</span>";
                    }

                    //return "<span><img src='" + data[datarow].user_name + "' width='80px;' height='80px;'>&nbsp;&nbsp;</span>" + data[datarow].title + "</span>";
                }
            },
            {
                title: '操作',
                dataIndex: 'action',
                dataSource: function (data, datarow, gridobj, current_column) {
                    return "<a class='editmodal' href='#' data-toggle='modal' data-target='#myModal' act_id="+data[datarow].doctorapply_id+"><span style='color: #6ab7ad;'>" + " 查看详情" + "</span></a>";

                }
            }
        ];
        //数组初始化
        function dataInit(realname,status) {

            grid = new Grid("/api/doctorApplieslist", $("#table_my_toast"), $("#paging_my_toast"), 1, 10, columns, get_input_param(realname,status), 10);
            //初始化
            grid.Init();

        }

        //获取输入框内容
        function get_input_param(realname,status) {
            var param="realname="+realname+"&status="+status;
            return param;
        }
        dataInit(realname,status);
    }

    var doctorapply_id;
    //详情按钮
    $(document).on("click",".editmodal",function(){

        var act_id=$(this).attr("act_id");
        doctorapply_id=act_id;
        if(act_id){

            //获得申请详情
            var pamars={};
            pamars.doctorapply_id=doctorapply_id;
            IO.post("/api/doctor_applies",pamars,function(d){
                if(d.code=="0"){
                    if(d.data.status==0){//待审核
                        btnShow();
                    }
                    else{
                        btnHide();
                    }
                    $("#realname2").val(d.data.realname);//真实姓名
                    $("#idcard").val(d.data.idcard);//身份证号
                    $("#bankcard").val(d.data.card_num);//银行卡号
                    $("#province").val(province_nameShow(d.data.province_id));//省
                    $("#city").val(city_nameShow(d.data.province_id,d.data.city_id));//市
                    $("#area").val(area_nameShow(d.data.province_id,d.data.city_id,d.data.area_id));//区
                    if(d.data.hospital==null){
                        $("#hospitalName").val("undefined");
                    }else{
                        $("#hospitalName").val(d.data.hospital.hospital_name);//医院全称
                    }
                    if(d.data.department==null){
                        $("#department").val("undefined");
                    }else{
                        $("#department").val(d.data.department.department_name);//科
                    }
                    if(d.data.depart_phone==null){
                        $("#departmentTel").val("undefined");
                    }else{
                        $("#departmentTel").val(d.data.depart_phone);//科室电话
                    }

                    $(".addimage1").attr( "src",d.data.pic_neg); //手持证件照
                    $(".addimage2").attr( "src",d.data.pic_pos );//正面免冠照
                    $("#goodCase").val(d.data.specialty);//擅长病症
                    $("#txtShow").val(d.data.introduction);//个人简介
                    $("#imgShow1").attr( "src",d.data.pic_neg);
                    $("#imgShow2").attr( "src",d.data.pic_pos);
                }
            });

            //获得医院所有医生类型
            var pamars2={};
            IO.post("/api/doctorTypeAll",pamars2,function(d){
                if(d.code=="0"){
                    $("#select_doctor").empty();
                    $("#select_doctor").append("<option value='-1' selected>请选择</option>");
                    for (var i=0;i< d.data.length;i++){
                        $("#select_doctor").append("<option value='"+d.data[i].doctor_type_id+"'>"+d.data[i].doctor_type_name+"</option>");
                    }
                }
            });


            //先查数据绑定到页面上,然后手动调用modal
            $('#myModaledit').modal();
        }
    });

    //拒绝按钮
    $(document).on("click","#refuse",function(){

        var pamars={};
        pamars.doctorapply_id=doctorapply_id;
        pamars.status=2;//1=通过 2=拒绝
        pamars.remark=$("#txtShow2").val();//拒绝时的理由
        pamars.newcard_ability="-1"//建卡能力
        if(pamars.remark==""){
            alert("请填写拒绝时的理由");
            return;
        }

        IO.post("/api/doctor_applies_Auditing",pamars,function(d){
            if(d.code=="0"){
                bootbox.confirm("拒绝成功",function(result){
                    $('#myModal').modal("hide");
                    var realname= $("#realname").val();
                    var status=$("#status").val();
                    if(status=="-1"){
                        status="";
                    }
                    gettable(realname,status);
                });

            }
        });
    });

    //通过按钮
    $(document).on("click","#pass",function(){

        var pamars={};
        pamars.doctorapply_id=doctorapply_id;
        pamars.doctor_type_id=$("#select_doctor").val() ;//拿到选中项的值
        pamars.newcard_ability=$("#newcard_ability").val()//建卡能力
        if( pamars.doctor_type_id=="-1"){
            alert("请选择医生类别");
            return;
        }
        if(pamars.newcard_ability=="-2"){
            alert("请选择建卡能力");
            return;
        }
        pamars.status=1;//1=通过 2=拒绝
        pamars.remark="";//拒绝时的理由
        IO.post("/api/doctor_applies_Auditing",pamars,function(d){
            if(d.code=="0"){
                bootbox.confirm("通过成功",function(result){
                    $('#myModal').modal("hide");
                    var realname= $("#realname").val();
                    var status=$("#status").val();
                    if(status=="-1"){
                        status="";
                    }
                    gettable(realname,status);
                });

            }
        });

    });


    //根据省id获得省名称
    function province_nameShow(province_id){
        var provinceList = JSON.parse($("#provinceA").val());
        var  obj;
        for (obj in provinceList) {
            if (provinceList[obj].code == province_id) {
                return provinceList[obj].name;
            }
        }
    }

    //根据省id市id获得市名称
    function city_nameShow(province_id,city_id){
        var provinceList = JSON.parse($("#provinceA").val());
        var  obj;
        for (obj in provinceList) {
            if (provinceList[obj].code == province_id) {
                var obj1;
                for(obj1 in provinceList[obj].cityList){
                   if(provinceList[obj].cityList[obj1].code==city_id){
                    return provinceList[obj].cityList[obj1].name;
                   }
                }
            }
        }
    }

    //根据省id市id区id获得区名称
    function area_nameShow(province_id,city_id,area_id){
        var provinceList = JSON.parse($("#provinceA").val());
        var  obj;
        for (obj in provinceList) {
            if (provinceList[obj].code == province_id) {
                var obj1;
                for(obj1 in provinceList[obj].cityList){
                    if(provinceList[obj].cityList[obj1].code==city_id){
                        var obj2;
                        for(obj2 in provinceList[obj].cityList[obj1].areaList){
                            if( provinceList[obj].cityList[obj1].areaList[obj2].code==area_id){
                            return provinceList[obj].cityList[obj1].areaList[obj2].name;
                            }
                        }
                    }
                }
            }

        }
    }

    //待审核显示通过,拒绝按钮
    function  btnHide(){
        $("#hide1").hide();
        $("#hide2").hide();
        $("#hide3").hide();
    }
    function btnShow(){
        $("#hide1").show();
        $("#hide2").show();
        $("#hide3").show();
    }

    //鼠标移动事件
    $(".addimage1").mousemove(function(){
        $("#myModal2").modal('show');
    });
    $(".addimage2").mousemove(function(){
        $("#myModal3").modal('show');
    });
});

$("#select_doctor").on("change",function() {
    //医生类型1=普通医生，2=省市级，3=名医
    if($("#select_doctor").val()=="1"){//普通医生

        $("#newcard_ability").empty();
        $("#newcard_ability").append("<option value='-2' selected>请选择</option>");
        $("#newcard_ability").append("<option value='1'>小卡</option>");
        $("#newcard_ability").append("<option value='2'>大卡</option>");
        $("#newcard_ability").append("<option value='-1'>不能建卡</option>");

    }else if($("#select_doctor").val()=="2"){//省市级
        $("#newcard_ability").empty();
        $("#newcard_ability").append("<option value='-2' selected>请选择</option>");
        $("#newcard_ability").append("<option value='1'>小卡</option>");
        $("#newcard_ability").append("<option value='2'>大卡</option>");
        $("#newcard_ability").append("<option value='-1'>不能建卡</option>");

    }else if($("#select_doctor").val()=="3"){//名医

        $("#newcard_ability").empty();
        $("#newcard_ability").append("<option value='-2' selected>请选择</option>");
        $("#newcard_ability").append("<option value='-1'>不能建卡</option>");
    }

});